home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / HELPUTIL.PY < prev    next >
Encoding:
Text File  |  2000-05-26  |  15.2 KB  |  500 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Help system support module"""
  65.  
  66. __version__='$Revision: 1.7 $'[11:-2]
  67.  
  68.  
  69. import Globals, Acquisition
  70. import StructuredText.StructuredText
  71. import sys, os, string, regex
  72.  
  73.  
  74. stx_class=StructuredText.StructuredText.HTML
  75.  
  76.  
  77. class HelpBase(Acquisition.Implicit):
  78.     """ """
  79.     def __bobo_traverse__(self, REQUEST, name=None):
  80.         # A sneaky trick - we cant really _have_ an index_html
  81.         # because that would often hide the index_html of a
  82.         # wrapped object ;(
  83.         if name=='index_html':
  84.             return self.hs_index
  85.         return getattr(self, name)
  86.  
  87.     def __len__(self):
  88.         return 1
  89.  
  90.  
  91. class object(Acquisition.Implicit):
  92.     def __init__(self, name, ob, op=None):
  93.         self._name=name
  94.         self._obj_=ob
  95.         self._obp_=op
  96.  
  97.     def __getattr__(self, name):
  98.         return getattr(self.__dict__['_obj_'], name)
  99.  
  100.     def __len__(self):
  101.         return 1
  102.  
  103.     def get_id(self):
  104.         return id(self._obj_)
  105.  
  106.     def get_name(self):
  107.         return self._name
  108.  
  109.     def get_type(self):
  110.         return type(self._obj_).__name__
  111.  
  112.     def get_value(self):
  113.         return self._obj_
  114.  
  115.     def get_docstring(self):
  116.         if hasattr(self._obj_, '__doc__'):
  117.             doc=self._obj_.__doc__
  118.             if not doc: doc=''
  119.             return doc
  120.         return ''
  121.  
  122.     def get_docstring_html(self):
  123.         doc=self.get_docstring()
  124.         if string.find(doc, '\n\n') > -1:
  125.             doc=string.split(doc, '\n\n')
  126.             if len(doc) > 1:
  127.                 doc[1]=string.strip(doc[1])
  128.             doc=string.join(doc, '\n\n')
  129.         
  130.         return str(stx_class(doc))
  131.  
  132.     def version(self):
  133.         if hasattr(self._obj_, '__version__'):
  134.             return self._obj_.__version__
  135.  
  136.     tpId   =get_name
  137.     tpURL  =get_name
  138.     __str__=get_name
  139.  
  140.  
  141. class moduleobject(object):
  142.     def get_file(self):
  143.         if hasattr(self._obj_, '__file__'):
  144.             return self._obj_.__file__
  145.  
  146.     def get_modules(self):
  147.         data=[]
  148.         for name, ob in self._obj_.__dict__.items():
  149.             if is_module(ob) and _chModule(name, ob):
  150.                 data.append(moduleobject(name, ob, self))
  151.         return data
  152.  
  153.     def get_classes(self):
  154.         data=[]
  155.         for name, ob in self._obj_.__dict__.items():
  156.             if is_class(ob) and _chClass(name, ob):
  157.                 data.append(classobject(name, ob, self))
  158.         return data
  159.  
  160.  
  161. class classobject(object):
  162.     def get_metatype(self):
  163.         try: return self._obj_.meta_type
  164.         except:
  165.             t, v, tb = sys.exc_info()
  166.             return '%s %s' % (t, v)
  167.     
  168.     def get_module(self):
  169.         if hasattr(self._obj_, '__module__'):
  170.             module=sys.modules[self._obj_.__module__]
  171.             return moduleobject(module.__name__, module)
  172.         
  173.     def get_file(self):
  174.         return self.get_module().get_file()
  175.  
  176.     def get_bases(self):
  177.         bases=[]
  178.         if hasattr(self._obj_, '__bases__'):
  179.             for base in self._obj_.__bases__:
  180.                 bases.append(classobject(base.__name__, base))
  181.         return bases
  182.  
  183.     def get_base_list(self, list=None):
  184.         if list is None: list=[]
  185.         list.append(self)
  186.         for base in self.get_bases():
  187.             list=base.get_base_list(list)
  188.         return list
  189.  
  190.     def get_methods(self):
  191.         keys=self._obj_.__dict__.keys()
  192.         dict=self._obj_.__dict__
  193.         keys.sort()
  194.         methods=[]
  195.         for name in keys:
  196.             ob=dict[name]
  197.             if is_method(ob) and _chMethod(name, ob):
  198.                methods.append(methodobject(name, ob, self))
  199.         return methods
  200.  
  201.     def get_method_dict(self, dict=None):
  202.         if dict is None:
  203.             dict={}
  204.         dup=dict.has_key
  205.         for method in self.get_methods():
  206.             name=method.get_name()
  207.             if not dup(name):
  208.                 dict[name]=method
  209.         for base in self.get_bases():
  210.             dict=base.get_method_dict(dict)
  211.         return dict
  212.  
  213.     def get_method_list(self):
  214.         dict=self.get_method_dict()
  215.         keys=dict.keys()
  216.         keys.sort()
  217.         list=[]
  218.         for key in keys:
  219.             list.append(dict[key])
  220.         del dict
  221.         return list
  222.  
  223. ##     def obAttributes(self):
  224. ##         # Return list of class attributes
  225. ##         keys=self._obj_.__dict__.keys()
  226. ##         dict=self._obj_.__dict__
  227. ##         keys.sort()
  228. ##         attrs=[]
  229. ##         for name in keys:
  230. ##             ob=dict[name]
  231. ##             if _isAttribute(ob) and _chAttribute(name, ob):
  232. ##                 attrs.append(AttributeObject(name, ob, self))
  233. ##         return attrs
  234.  
  235. ##     def obAttributeDict(self, dict=None):
  236. ##         # Return dict of attrs in class and superclasses
  237. ##         if dict is None:
  238. ##             dict={}
  239. ##             root=1
  240. ##         else: root=0
  241. ##         dup=dict.has_key
  242. ##         for attr in self._obj_Attributes():
  243. ##             name=attr.obName()
  244. ##             if not dup(name):
  245. ##                 dict[name]=attr
  246. ##         for base in self._obj_Bases():
  247. ##             dict=base.obAttributeDict(dict)
  248. ##         return dict
  249.  
  250. ##     def obAttributeList(self):
  251. ##         # Return list of attrs in class and superclasses
  252. ##         dict=self._obj_AttributeDict()
  253. ##         keys=dict.keys()
  254. ##         keys.sort()
  255. ##         list=[]
  256. ##         append=list.append
  257. ##         for name in keys:
  258. ##             append(dict[name])
  259. ##             del dict
  260. ##         return list
  261.  
  262.  
  263.  
  264.  
  265. pre_match=regex.compile('[A-Za-z0-9_]*([^)]*)[ -]*').match #TS
  266. sig_match=regex.compile('[A-Za-z0-9_]*([^)]*)').match #TS
  267.  
  268. class methodobject(object):
  269.  
  270.     def get_class(self):
  271.         return self._obp_
  272.  
  273.     def get_module(self):
  274.         return self.get_class().get_module()
  275.  
  276.     def get_file(self):
  277.         return self.get_module().get_file()
  278.  
  279.     def get_docstring(self):
  280.         func=self._obj_
  281.         doc=''
  282.         if hasattr(func, 'im_func'):
  283.             func=func.im_func
  284.         if hasattr(func, '__doc__'):
  285.             doc=func.__doc__
  286.             if not doc: doc=''
  287.             doc=string.strip(doc)
  288.         if hasattr(func, 'func_code'):
  289.             if hasattr(func.func_code, 'co_varnames'):
  290.                 return doc
  291.         n=pre_match(doc)
  292.         if n > -1:
  293.             return doc[n:]
  294.         return doc
  295.  
  296.     def get_signaturex(self):
  297.         name=self._name
  298.         func=self._obj_
  299.         method=None
  300.  
  301.         if hasattr(func, 'im_func'):
  302.             method=1
  303.             func=func.im_func
  304.  
  305.         # Normal functions
  306.         if hasattr(func, 'func_code'):
  307.             if hasattr(func.func_code, 'co_varnames'):
  308.                 args=map(lambda x: x,
  309.                      func.func_code.co_varnames[:func.func_code.co_argcount])
  310.                 ndefaults=func.func_defaults
  311.                 ndefaults=ndefaults and len(ndefaults) or 0
  312.                 if '__ick__' in args:
  313.                     nick=len(args)-args.index('__ick__')
  314.                     args=args[:-nick]
  315.                     ndefaults=ndefaults-nick
  316.                 if ndefaults > 0:
  317.                     args[-ndefaults]='['+args[-ndefaults]
  318.                     args[-1]=args[-1]+']'
  319.                 if method: args=args[1:]
  320.                 if name=='__call__':
  321.                     name='Call Operation'
  322.                 return '%s(%s)' % (name, string.join(args,', '))
  323.  
  324.         # Other functions - look for something that smells like
  325.         # a signature at the beginning of the docstring.
  326.         if hasattr(func, '__doc__'):
  327.             doc=func.__doc__
  328.             if not doc: doc=''
  329.             doc=string.strip(doc)
  330.             n=sig_match(doc)
  331.             if n > -1:
  332.                 return doc[:n]
  333.         return '%s()' % name
  334.                       
  335.  
  336.     def get_signature(self):
  337.         try: return self.get_signaturex()
  338.         except:
  339.             t, v, tb=sys.exc_info()
  340.             return '%s %s' % (t, v)
  341.  
  342.  
  343. ## class AttributeObject(_ob_):
  344. ##     def obClass(self):
  345. ##         return self.op
  346.  
  347. ##     def obModule(self):
  348. ##         return self.obClass().obModule()
  349.  
  350. ##     def obFile(self):
  351. ##         return self.obModule().obFile()
  352.  
  353. ## class InstanceObject(_ob_):
  354. ##     def obClass(self):
  355. ##         # Return the class for this instance
  356. ##         c=self._obj_.__class__
  357. ##         return ClassObject(c.__name__, c)
  358.  
  359. ##     def obClassList(self):
  360. ##         # Return list of all superclasses
  361. ##         return self._obj_Class().obClassList()
  362.  
  363. ##     def obMethods(self):
  364. ##         # Return list of instance methods
  365. ##         keys=self._obj_.__dict__.keys()
  366. ##         dict=self._obj_.__dict__
  367. ##         keys.sort()
  368. ##         methods=[]
  369. ##         for name in keys:
  370. ##             ob=dict[name]
  371. ##             if _isMethod(ob) and _chMethod(name, ob):
  372. ##                methods.append(MethodObject(name, ob))
  373. ##         return methods
  374.  
  375. ##     def obMethodDict(self):
  376. ##         # Return dict of instance and superclass methods
  377. ##         dict=self._obj_Class().obMethodDict()
  378. ##         for method in self._obj_Methods():
  379. ##             dict[method.obName()]=method
  380. ##         return dict
  381.             
  382. ##     def obMethodList(self):
  383. ##         # Return list of instance and superclass methods
  384. ##         dict=self._obj_MethodDict()
  385. ##         keys=dict.keys()
  386. ##         keys.sort()
  387. ##         list=[]
  388. ##         append=list.append
  389. ##         for name in keys:
  390. ##             append(dict[name])
  391. ##         return list
  392.  
  393. ##     def obAttributes(self):
  394. ##         # Return list of instance attributes
  395. ##         keys=self._obj_.__dict__.keys()
  396. ##         dict=self._obj_.__dict__
  397. ##         keys.sort()
  398. ##         attrs=[]
  399. ##         for name in keys:
  400. ##             ob=dict[name]
  401. ##             if _isAttribute(ob) and _chAttribute(name, ob):
  402. ##                 attrs.append(AttributeObject(name, ob, self))
  403. ##         return attrs
  404.     
  405. ##     def obAttributeDict(self):
  406. ##         # Return dict of instance and superclass attributes
  407. ##         dict=self._obj_Class().obAttributeDict()
  408. ##         for attr in self._obj_Attributes():
  409. ##             dict[attr.obName()]=attr
  410. ##         return dict
  411.             
  412. ##     def obAttributeList(self):
  413. ##         # Return list of instance and superclass attributes
  414. ##         dict=self._obj_AttributeDict()
  415. ##         keys=dict.keys()
  416. ##         keys.sort()
  417. ##         list=[]
  418. ##         append=list.append
  419. ##         for name in keys:
  420. ##             append(dict[name])
  421. ##         return list
  422.  
  423.  
  424.  
  425.  
  426. _classtypes=(type(Globals.HTML),
  427.              type(Globals.Persistent),
  428.             )
  429.  
  430. _methodtypes=(type([].sort),
  431.               type(Globals.default__class_init__),
  432.               type(Globals.HTML.manage_edit),
  433.               type(Globals.HTML.__changed__),
  434.               type(Globals.MessageDialog.manage_edit),
  435.              )
  436.  
  437. def is_module(ob):
  438.     return type(ob)==type(sys)
  439.  
  440. def is_class(ob):
  441.     return type(ob) in _classtypes
  442.  
  443. def is_method(ob):
  444.     if type(ob) in _methodtypes or hasattr(ob, 'func_code'):
  445.         return 1
  446.     return 0
  447.  
  448. def is_attribute(ob):
  449.     return not is_method(ob)
  450.  
  451.  
  452.  
  453. def _chModule(name, ob):
  454.     if name[0]=='_':
  455.         return 0
  456.     return 1
  457.  
  458. def _chClass(name, ob):
  459.     if name[0]=='_':
  460.         return 0
  461.     return 1
  462.  
  463. def _chMethod(name, ob):
  464.     if name[0]=='_':
  465.         return 0
  466.     return 1
  467.  
  468. def _chAttribute(name, ob):
  469.     if name[0]=='_':
  470.         return 0
  471.     return 1
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.